// pages/movie/movie.js
const {
    fetchCity,
    fetchHotMovie,
    fetchwaitMovie,
    fetchgoodsMovie,
    fetchrecommendMovie
} = require('../../api/index.js')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        city: "汕头市",
        tabs: [{
                title: '热映',
            },
            {
                title: '待映',
            },
            {
                title: '影院',
            }, {
                title: '猫眼推荐'
            }

        ],
        activeIndex: 0,
        isAgreeLocation: true,
        firstIn: true,
        mpheight: 0,
        movieList: [], //热映
        waitmovie: {}, //待映
        htmlSnip: '', //好评
        recommendMovie:[],//推荐
        sortId:20,//推荐条数
        showType:1,//推荐页面
        limit:10,//推荐
        page: 1,
        pagesize: 10,
        windowHeight: ''
    },
    onChange(e) {
        console.log(e.detail);
        let {
            index
        } = e.detail;
        this.setData({
            activeIndex: index
        })
    },
    onLoad() {
        // 获取所在城市
        let city = wx.getStorageSync('city')
        if (city) {
            this.setData({
                city
            })
        } else {
            // 默认定位用户所在位置
            this.getUserLocation()
        }
        // 获取热映电影
        this._fetchHotMovie()
        //获取待映数据
        this._fetchwaitMovie()
        //获取好评数据
        this._fetchgoodsMovie()
        //猫眼推荐
        this._fetchrecommendMovie()
        // 获取节点高度
        this.getElHeight()
    },
    getElHeight() {
        let query = wx.createSelectorQuery();
        query.select('.header').boundingClientRect(rect => {
            let height = rect.height;
            this.setData({
                windowHeight: wx.getSystemInfoSync().windowHeight - height
            })
        }).exec();
    },
    //热映数据
    async _fetchHotMovie() {

        let _getHotMovie = await fetchHotMovie(this.data.page, this.data.pagesize)
        //判断电影内容是否加载完毕
        if (_getHotMovie.length < this.data.pagesize) {
            this.setData({
                More: false
            })
            wx.showToast({
                title: '数据已经加载完毕别逼我哦',
            })
            return
        }
        _getHotMovie = _getHotMovie.map(item => {
            item.img = item.img.replace(/\/w\.h/, '')
            return item;
        })
        this.setData({
            movieList: _getHotMovie
        })
    },
    //待映数据
    async _fetchwaitMovie() {
        let {
            movieList
        } = await fetchwaitMovie()
        movieList = movieList.map(item => {
            item.img = item.img.replace(/\/w\.h/, '')
            return item;
        })
        this.setData({
            waitmovie: movieList
        })
    },
    //好评数据
    async _fetchgoodsMovie() {
        let htmlSnip = await fetchgoodsMovie()
        console.log(htmlSnip);
        this.setData({
            htmlSnip
        })

    },
    //猫眼推荐
    async _fetchrecommendMovie() {
        let recommendMovie = await fetchrecommendMovie(this.data.showType, this.data.limit, this.data.sortId)
        this.setData({
            recommendMovie
        })
    },

    getUserLocation() {
        // 1. 获取经纬度（同意success=》获取到经纬度），若拒绝fail,则需要引导用户进入到授权页面进行同意位置授权
        // 2. 获取到经纬度之后，则直接调用百度api接口，获取所在的位置信息（城市），并存入到本地存储
        // getLocation弹窗无论拒绝还是允许仅会弹窗一次
        let _this = this;
        wx.getLocation({
            type: 'wgs84',
            isHighAccuracy: true,
            async success(res) {
                // 允许
                console.log('允许')
                let {
                    latitude,
                    longitude
                } = res
                let data = await fetchCity(latitude, longitude)
                let city = data.result.addressComponent.city;
                wx.setStorageSync('city', city)
                _this.setData({
                    city
                })
            },
            fail(err) {
                // 拒绝 引导用户进入到授权页面 openSetting
                console.log('拒绝 引导用户进入到授权页面 openSetting');
                _this.setData({
                    isAgreeLocation: false
                })
            }
        })
    },
    onHide() {
        console.log('不可见')
        this.setData({
            firstIn: false
        })
    },
    onShow() {
        console.log('可见')
        // 如果不是第一次进入则不要执行
        if (this.data.firstIn) {
            return;
        }
        let _this = this;
        wx.getSetting({
            success(res) {
                if (res.authSetting['scope.userLocation']) {
                    // 用户自己同意
                    console.log('用户同意');
                    _this.getUserLocation()
                    _this.setData({
                        isAgreeLocation: true
                    })
                } else {
                    // 用户拒绝
                    console.log('用户不同意');
                    wx.showToast({
                        title: '请同意位置授权 以便获取更好的服务'
                    })
                    _this.setData({
                        isAgreeLocation: false
                    })

                }
            }
        })
    }
})